package com.gxuwz.zhangjiahui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.zhangjiahui.entity.Dep;
import com.gxuwz.zhangjiahui.entity.Instructor;
import com.gxuwz.zhangjiahui.entity.Limit;
import com.gxuwz.zhangjiahui.entity.Role;
import com.gxuwz.zhangjiahui.service.IDepService;
import com.gxuwz.zhangjiahui.service.ILimitService;
import com.gxuwz.zhangjiahui.service.IRoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiahui
 * @since 2021-06-20
 */
@RestController
@RequestMapping("/dep")
public class DepController {

    @Autowired
    private IDepService iDepService;


    /**
     * 查询全部信息
     * @param modelAndView
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/findDep")
    public ModelAndView findDep(ModelAndView modelAndView, Page page, Integer pageNumber,
                                    @Param("depId") String depId,
                                    HttpServletRequest request, HttpServletResponse response) {
        if(request.getSession().getAttribute("nowResult") != null){
            if (!request.getSession().getAttribute("nowResult").equals("now")) {
                request.getSession().setAttribute("result", "");
            }
            request.getSession().setAttribute("nowResult", "");
        }
        // 可以通过 wrapper 进行筛选!!!
        QueryWrapper<Dep> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("dep_id");
        String[] keysWord = null;
        // 对User进行模糊查询!!!
        if(depId != null && depId != ""){
            wrapper.like("dep_id", depId);
            request.getSession().setAttribute("depId", depId);
        }else {
            request.getSession().setAttribute("depId", "");
        }
        // Current,页码 + Size,每页条数
        if(pageNumber == null){
            page.setCurrent(1);
        }else {
            page.setCurrent((long)pageNumber);
        }
        // 默认每页6行数据！
        page.setSize(6);
        // 调用分页查询方法！!
        IPage<Dep> depIPage = iDepService.selectDepPage(page, wrapper);
        // 存放一个数组用来让foreach遍历
        int[] pagesList = new int[(int)depIPage.getPages()];
        for(int i=0; i< (int)depIPage.getPages(); i++){
            pagesList[i] = i+1;
        }
        modelAndView.addObject("pagesList", pagesList);
        modelAndView.addObject("numberPages", depIPage.getTotal());
        // 存放page，内有当前页数
        modelAndView.addObject("page", page);
        System.out.println("总条数"+depIPage.getTotal());
        System.out.println("总页数"+depIPage.getPages());
        // 存放总页数
        modelAndView.addObject("pages", (int)depIPage.getPages());
        List<Dep> depList = depIPage.getRecords();
        System.out.println("depList = "+depList);
        modelAndView.addObject("depList", depList);

        modelAndView.setViewName("dep/dep_list");
        return modelAndView;
    }


    /**
     * 添加/修改 信息
     * @param dep
     * @return
     */
    @PostMapping(value = "/addEditDep")
    public ModelAndView addEditDep(Dep dep, HttpServletRequest request, ModelAndView modelAndView) {
        Dep dep1 = iDepService.findDepById(dep);
        System.out.println("dep = " + dep);
        System.out.println("dep1 = " + dep1);
        // 新增信息
        if(dep1 == null){
            System.out.println("进入新增");
            try {
                iDepService.addDep(dep);
                request.getSession().setAttribute("result", "addTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "addFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        // 修改用户信息
        if(dep1 != null){
            System.out.println("进入修改");
            try {
                iDepService.updateDepById(dep);
                request.getSession().setAttribute("result", "editTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "editFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/dep/findDep");
        return modelAndView;
    }



    /**
     * 删除信息
     * @param depId
     * @return
     */
    @GetMapping(value = "/deleteDep")
    public ModelAndView deleteDep(HttpServletRequest request, ModelAndView modelAndView,
                                         @Param("depId") String depId) {
        Dep dep = new Dep();
        dep.setDepId(depId);
        try {
            iDepService.deleteDepById(dep);
            request.getSession().setAttribute("result", "deleteTrue");
            request.getSession().setAttribute("nowResult", "now");
        }catch (Exception e){

        }
        modelAndView.setViewName("redirect:/dep/findDep");
        return modelAndView;
    }
}
